<?php
/**
 * Created by PhpStorm.
 * User: moting
 * Date: 2017/6/13
 * Time: 0:48
 */?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title>数据列表页面</title>
    <link href="<?php echo Asset::get_file('layui/css/layui.css','plugin'); ?>" rel="stylesheet" />
    <script src="<?php echo Asset::get_file('jquery-1.8.2.min.js','js'); ?>"></script>
    <style>
        .custom-td-input-edit{
            padding-bottom: 0!important;
            padding-top: 0!important;
        }
        .custom-upload-div{
            width: 190px;
            height: 270px;
            text-align:center;
            background-image: url('<?php
            if(isset($item['picture_src_path'])){
                echo $item['picture_src_path'];
            }else{
                echo Asset::get_file('default_upload.png','img');
            }?>');
        }
        .layui-box{
            margin-top: 50%;
            background-color: rgba(0,0,0,.2);
            color: rgba(255,255,255,1);
        }
        .layui-upload-file{
            z-index: 10;
        }
    </style>
</head>
<body>
<table style="" class="layui-table" lay-even>
    <colgroup>
        <col width="200"/>
        <col width="350"/>
    </colgroup>
    <tbody>
    <tr>
        <td><div class="custom-upload-div">
                <input type="file" name="file" class="layui-upload-file" id="formUploadFile"><input
                        type="hidden" value="" id="formMovieUrl">
            </div></td>
        <td style='font-size: 10px;'>
            <table>
                <tbody>
                <tr>
                    <td><span>视频名称</span></td>
                    <td class="custom-td-input-edit"><input id="formMovieName" type="text" class="layui-input" value="" autocomplete="on" placeholder="请输入视频的全称"></td>
                </tr>
                <tr>
                    <td><span>导演</span></td>
                    <td class="custom-td-input-edit"><input id="formDirectorName" type="text" class="layui-input" value="" autocomplete="on" placeholder="请输入视频的导演"></td>
                </tr>
                <tr>
                    <td><span>主演</span></td>
                    <td class="custom-td-input-edit"><input id="formActorName" type="text" class="layui-input" value="" autocomplete="on" placeholder="请输入视频的主要演员"></td>
                </tr>
                <tr>
                    <td><span>制片国家</span></td>
                    <td class="custom-td-input-edit"><input id="formStateName" type="text" class="layui-input" value="" autocomplete="on" placeholder="请输入视频的出版国家"></td>
                </tr>
                <tr>
                    <td><span>视频类型</span></td>
                    <td class="custom-td-input-edit">
                        <form class="layui-form" action="" style="height: 38px;">
                            <div class="layui-form-item">
                                <div class="layui-input-inline">
                                    <select name="quiz1" id="formMovieType">
                                        <option value="">请选择</option>
                                        <option value="film">电影</option>
                                        <option value="tv" selected="">电视剧</option>
                                        <option value="variety">综艺</option>
                                        <option value="anime">动漫</option>
                                    </select>
                                </div>
                            </div>
                        </form>
                    </td>
                </tr>
                <tr>
                    <td><span>上映日期</span></td>
                    <td class="custom-td-input-edit">
                        <div class="layui-input-inline">
                            <input class="layui-input" placeholder="开始日" id="LAY_demorange_s" value="" style="width: 200px">
                        </div>
                    </td>
                </tr>
                <tr>
                    <td><span>评分</span></td>
                    <td class="custom-td-input-edit"><input id="formGradeNum" type="text" class="layui-input" value="" autocomplete="on" placeholder="请输入视频的评分(豆瓣)"></td>
                </tr>
                </tbody>
            </table>
        </td>
    </tr>
    <tr>
        <td colspan="2">
            视频简介
        </td>
    </tr>
    <tr>
        <td colspan="2">
                <textarea placeholder="请输入内容" class="layui-textarea" id="formMovieDetail"></textarea>
        </td>
    </tr>
    </tbody>
</table>
<div >
    &nbsp;&nbsp;<button type="button" class="layui-btn" id="fromSubmit">确定</button>
    &nbsp;&nbsp;<button type="button" class="layui-btn" id="formCancel">取消</button>
</div>
<!-- layui.js -->
<script src="<?php echo Asset::get_file('layui/layui.js','plugin'); ?>"></script>
<!-- layui规范化用法 -->
<script type="text/javascript">

    var URL_GETDATA = "<?php  echo Uri::create('manager/pagedata'); ?>";
    var URL_INSERTDATA = "<?php  echo Uri::create('manager/insert'); ?>";
    var URL_UPDATEDATA = "<?php  echo Uri::create('manager/update'); ?>";

    $('#fromSubmit').on('click',function () {
        var sendUrl = URL_INSERTDATA;
        if( $('#fromSubmit').html().toString() == "修改"){
            sendUrl = URL_UPDATEDATA;
        }
        var addArrForm = getForm();
        if(addArrForm !== false){
            $.post(sendUrl, addArrForm, function (res) {
                if(res != 'error'){
                    console.log('success');
                    $('#formCancel').click();
                }else{
                    console.log('error');
                    $('#formCancel').click();
                }
            });
        }
    });

    //获取表单
    function getForm() {
        if($('#formMovieUrl').val() == ""){

        }else if($('#formMovieName').val() == ""){

        }else if($('#formDirectorName').val() == ""){

        }else if($('#formActorName').val() == ""){

        }else if($('#formStateName').val() == ""){

        }else if($('#formMovieType').val() == ""){

        }else if($('#LAY_demorange_s').val() == ""){

        }else if($('#formGradeNum').val() == ""){

        }else if($('#formMovieDetail').val() == ""){

        }else{
            if($('#formMovieDetail').val().length >= 168){
                $('#formMovieDetail').val($('#formMovieDetail').val().substr(0,168));
            }
            return {
                picture_src_path    : $('#formMovieUrl').val(),     // 图片路径
                movie_name          : $('#formMovieName').val(),    // 视频名字
                director_name       : $('#formDirectorName').val(), // 导演名字
                actor_name          : $('#formActorName').val(),    // 演员列表
                movie_type          : $('#formMovieType').val(),    // 视频类型
                state_name          : $('#formStateName').val(),    // 出版国家
                grade_num           : $('#formGradeNum').val(),     // 评分
                movie_detail        : $('#formMovieDetail').val(),  // 视频详情
                update_time         : $('#LAY_demorange_s').val()   // 上映时间
            }
        }
        return false;
    }


    //layui的加载
    layui.use(['laydate', 'form', 'upload'], function () {
        var form = layui.form();
        var laydate = layui.laydate;
        var layer = layui.layer;

        var hangleSign = "<?php echo $handle_type; ?>";

        if(hangleSign == "edit"){
            $.post(URL_GETDATA,{type:'getdata'},function (res) {
                $currentData = eval('(' + res + ')');
                setForm($currentData);
            });
        }

        var start = {
            max: '2099-06-16'
            ,istoday: false
            ,choose: function(datas){
                start.start = datas //将结束日的初始值设定为开始日
            }
        };

        $('#formCancel').on('click',function () {
            //当你在iframe页面关闭自身时
            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
            parent.layer.close(index); //再执行关闭
        });

        layui.upload({
            url: "<?php  echo Uri::create('manager/upload'); ?>" //上传接口
            ,success: function(res){ //上传成功后的回调
                $('.custom-upload-div')[0].style.background = 'url(' + res.url + ')';
                $('.layui-upload-file')[0].style.display = "hidden";
                $('#formMovieUrl').val(res.url);
                //console.log($('#formMovieUrl'));
            }
        });
        document.getElementById('LAY_demorange_s').onclick = function(){
            start.elem = this;
            laydate(start);
        }
        function setForm(item) {
            $('#fromSubmit').html('修改');
            $('.custom-upload-div')[0].style.background = 'url(' + item['picture_src_path'] + ')';
            $('#formMovieUrl').val(item['picture_src_path']);       // 图片路径
            $('#formMovieName').val(item['movie_name']);      // 视频名字
            $('#formDirectorName').val(item['director_name']);   // 导演名字
            $('#formActorName').val(item['actor_name']);      // 演员列表
            $('#formMovieType').val(item['movie_type']);      // 视频类型
            $('#formStateName').val(item['state_name']);      // 出版国家
            $('#formGradeNum').val(item['grade_num']);       // 评分
            $('#formMovieDetail').val(item['movie_detail']);    // 视频详情
            $('#LAY_demorange_s').val(item['create_time']);    // 上映时间
        }
    });
</script>
</body>